package com.rattsoftware.client;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Event that is triggered by a Refresh and indicates what type of refresh
 * should take place.
 * 
 * Also allows data to be passed along to the View consuming the event.
 * 
 */
public class RefreshableEvent implements Serializable {

	private static final long serialVersionUID = 7549150917841490121L;

	public static enum Type {
		CREATE_USER, UPDATE_USER, REFRESH_USERLIST, CREATE_FACULTY, UPDATE_FACULTY, 
		REFRESH_FACULTY_LIST, DELETE_FACULTY, CREATE_COURSE, UPDATE_COURSE, REFRESH_COURSE_LIST, 
		DELETE_COURSE, CREATE_COURSE_UNIT, UPDATE_COURSE_UNIT, REFRESH_COURSE_UNIT_LIST, 
		DELETE_COURSE_UNIT, CREATE_DEPARTMENT, UPDATE_DEPARTMENT, REFRESH_DEPARTMENT_LIST, 
		DELETE_DEPARTMENT, CREATE_STAFF, UPDATE_STAFF, REFRESH_STAFF_LIST, DELETE_STAFF,
		CREATE_ROOM, UPDATE_ROOM, REFRESH_ROOM_LIST, DELETE_ROOM,
		CREATE_TIMETABLE, UPDATE_TIMETABLE, REFRESH_TIMETABLE_LIST, DELETE_TIMETABLE
	};

	private Type eventType;
	private Map<String, Object> data = new HashMap<String, Object>();

	public RefreshableEvent(Type eventType) {
		this.eventType = eventType;
	}

	public RefreshableEvent(Type eventType, Object data) {
		this(eventType);
		this.data.put("data", data);
	}

	@SuppressWarnings("unchecked")
	public <X> X getData() {
		return (X) data.get("data");
	}

	@SuppressWarnings("unchecked")
	public <X> X getData(String key) {
		return (X) data.get(key);
	}

	public void setData(Object data) {
		this.data.put("data", data);
	}

	public void setData(String key, Object data) {
		this.data.put(key, data);
	}

	public Type getEventType() {
		return eventType;
	}
}
